package com.boss.salary.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.controller.SalaryGrantSignController;
import com.boss.salary.dao.ISalaryGrantSignDao;
import com.boss.salary.entity.SalaryGrantSign;
import com.boss.salary.service.ISalaryGrantSignService;
import com.boss.sys.utils.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SalaryGrantSignServiceImpl implements ISalaryGrantSignService {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(SalaryGrantSignController.class);

    @Autowired
    private ISalaryGrantSignDao iSalaryGrantSignDao;

    @Override
    public PageDTO<SalaryGrantSign> getSalaryGrantSignPage(PaginationDTO paginationDTO, Map<String, Object> queryParam) {

        List<SalaryGrantSign> salaryGrantSigns = iSalaryGrantSignDao.getSalaryGrantSign(paginationDTO, queryParam);
        List<SalaryGrantSign> resultList = new ArrayList<>();
        if(salaryGrantSigns.size()>0){
            int no = 0;
            int id = 0;
            String oldSalaCardBank="";
            List<SalaryGrantSign>childrenList=new ArrayList<>();
            for (SalaryGrantSign salaryGrantSign : salaryGrantSigns) {
                ++id;
               String salarCardBank=salaryGrantSign.getSalaCardBank();
                salaryGrantSign.setId(String.valueOf(id));
               if(oldSalaCardBank.equals(salarCardBank)){
                   resultList.get(resultList.size()-1).getChildren().add(salaryGrantSign);
               }else{
                     if(resultList.size()>0){
                         childrenList= new ArrayList<>();
                     }
                   ++no;
                   ++id;
                   oldSalaCardBank=salarCardBank;
                   SalaryGrantSign salaryGrantSign1 = new SalaryGrantSign();
                   salaryGrantSign1.setSalaCardBank(salaryGrantSign.getSalaCardBank());
                   salaryGrantSign1.setNo(String.valueOf(no));
                   salaryGrantSign1.setId(String.valueOf(id));
                   childrenList.add(salaryGrantSign);
                   salaryGrantSign1.setChildren(childrenList);
                   resultList.add(salaryGrantSign1);
               }
            }
        }

        List<SalaryGrantSign> salarySumByBank = iSalaryGrantSignDao.getSalarySumByBank(queryParam);

        for (SalaryGrantSign salaryGrantSign : resultList) {
            String bandCode=salaryGrantSign.getSalaCardBank();
            for (SalaryGrantSign grantSign : salarySumByBank) {
                if(grantSign.getSalaCardBank().equals(bandCode)){
                    salaryGrantSign.setPersonEdit(grantSign.getPersonEdit());
                    salaryGrantSign.setPersonEditLastMonth(grantSign.getPersonEditLastMonth());
                    salaryGrantSign.setPersonRetire(grantSign.getPersonRetire());
                    salaryGrantSign.setPersonRetireLastMonth(grantSign.getPersonRetireLastMonth());
                    salaryGrantSign.setPersonSum(grantSign.getPersonEdit()+grantSign.getPersonRetire());

                    salaryGrantSign.setRealEdit(grantSign.getRealEdit());
                    salaryGrantSign.setRealEditLastMonth(grantSign.getRealEditLastMonth());
                    salaryGrantSign.setRealRetire(grantSign.getRealRetire());
                    salaryGrantSign.setRealRetireLastMonth(grantSign.getRealRetireLastMonth());
                    salaryGrantSign.setRealSum(grantSign.getRealEdit()+grantSign.getRealRetire());

                    salaryGrantSign.setShouldCareer(grantSign.getShouldCareer());
                    salaryGrantSign.setShouldCareerLastMonth(grantSign.getShouldCareerLastMonth());
                    salaryGrantSign.setShouldRetire(grantSign.getShouldRetire());
                    salaryGrantSign.setShouldRetireLastMonth(grantSign.getShouldRetireLastMonth());
                    salaryGrantSign.setShouldSum(grantSign.getShouldCareer()+grantSign.getShouldRetire());
                }
            }
        }

        SalaryGrantSign combined = iSalaryGrantSignDao.getSalaryGrantSignCombined(queryParam);
        int total=resultList.size();
        if(resultList.size()>0){
            combined.setAgencyCode("本月合计");
            combined.setId("0");
            //统发人员小计
            combined.setPersonSum(combined.getPersonEdit()+combined.getPersonRetire());
            //实发工资小计
            combined.setRealSum(combined.getRealEdit()+combined.getRealRetire());
            //应发工资小计
            combined.setShouldSum(combined.getShouldCareer()+combined.getShouldRetire());
            resultList.add(0,combined);
        }
//int total = iSalaryGrantSignDao.getSalaryGrantSignNum(queryParam);
      PageDTO<SalaryGrantSign> pageDTO = new PageDTO<>();
        pageDTO.setRows(resultList);
        pageDTO.setTotal(total);
        return pageDTO;
    }
}
